<?php
require_once(SITE_ROOT . 'class/db/DB.class.php');
require_once(SITE_ROOT . 'class/bean/Country.class.php');
require_once(SITE_ROOT . 'class/bean/Division.class.php');
require_once(SITE_ROOT . 'class/bean/League.class.php');

class DBLeague {
	
	public function getLeaguesByDivision($division) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('SELECT `league#`, `name`, `update`, `country#`, `division#` FROM `tleague` WHERE `country#` = ? AND `division#` = ? ORDER BY `league#` ASC ')) {
			$countryID = $division->getCountry()->getCountryID();
			$divisionID = $division->getDivisionID();
			$stmt->execute(array($countryID, $divisionID));
			
			$stmt->bindColumn(1, $leagueID);
			$stmt->bindColumn(2, $name);
			$stmt->bindColumn(3, $update);
			$stmt->bindColumn(4, $countryID);
			$stmt->bindColumn(5, $divisionID);
			
			$leagues = new ArrayObject();
			
			while ($stmt->fetch()) {
				$league = new League();
				$league->setLeagueID($leagueID);
				$league->setName($name);
				$league->setUpdate(strtotime($update));
				$country = new Country();
				$country->setCountryID($countryID);
				$division = new Division();
				$division->setDivisionID($divisionID);
				$division->setCountry($country);
				$league->setDivision($division);
				$leagues->append($league);
			}
			return $leagues;
		}
	}
	
	public function getLeaguesByCountry($country) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('SELECT `league#`, `name`, `update`, `country#`, `division#` FROM `tleague` WHERE `country#` = ? ORDER BY `league#` ASC ')) {
			$stmt->execute(array($country->getCountryID()));
			
			$stmt->bindColumn(1, $leagueID);
			$stmt->bindColumn(2, $name);
			$stmt->bindColumn(3, $update);
			$stmt->bindColumn(4, $countryID);
			$stmt->bindColumn(5, $divisionID);
			
			$leagues = new ArrayObject();
			
			while ($stmt->fetch()) {
				$league = new League();
				$league->setLeagueID($leagueID);
				$league->setName($name);
				$league->setUpdate(strtotime($update));
				$division = new Division();
				$division->setDivisionID($divisionID);
				$division->setCountry($country);
				$league->setDivision($division);
				$leagues->append($league);
			}
			return $leagues;
		}
	}
	
	public function getLeaguesByLevel($level) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('SELECT `league#`, `name`, `update`, `country#`, `division#` FROM `tleague` WHERE `division#` = ?')) {
			$stmt->execute(array($level));
			
			$stmt->bindColumn(1, $leagueID);
			$stmt->bindColumn(2, $name);
			$stmt->bindColumn(3, $update);
			$stmt->bindColumn(4, $countryID);
			$stmt->bindColumn(5, $divisionID);
			
			$leagues = new ArrayObject();
			
			while ($stmt->fetch()) {
				$league = new League();
				$league->setLeagueID($leagueID);
				$league->setName($name);
				$league->setUpdate(strtotime($update));
				$country = new Country();
				$country->setCountryID($countryID);
				$division = new Division();
				$division->setDivisionID($divisionID);
				$division->setCountry($country);
				$league->setDivision($division);
				$leagues->append($league);
			}
			return $leagues;
		}
	}
	
	public function getLeague($leagueID) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('SELECT `league#`, `name`, `update`, `country#`, `division#` FROM `tleague` WHERE `league#` = ?')) {
			$stmt->execute(array($leagueID));
			
			$stmt->bindColumn(1, $leagueID);
			$stmt->bindColumn(2, $name);
			$stmt->bindColumn(3, $update);
			$stmt->bindColumn(4, $countryID);
			$stmt->bindColumn(5, $divisionID);
			
			$league = null;
			
			while ($stmt->fetch()) {
				$league = new League();
				$league->setLeagueID($leagueID);
				$league->setName($name);
				$league->setUpdate(strtotime($update));
				$country = new Country();
				$country->setCountryID($countryID);
				$division = new Division();
				$division->setDivisionID($divisionID);
				$division->setCountry($country);
				$league->setDivision($division);
			}
			return $league;
		}
	}
	
	public function saveLeague($league) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('INSERT INTO `tleague` (`country#`, `division#`, `league#`, `name`, `update`) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE `name`=VALUES(`name`), `update`=VALUES(`update`)')) {
			$countryID = $league->getDivision()->getCountry()->getCountryID();
			$divisionID = $league->getDivision()->getDivisionID();
			$leagueID = $league->getLeagueID();
			$name = $league->getName();
			$update = date('Y-m-d H:i:s', $league->getUpdate());
			
			$stmt->execute(array($countryID, $divisionID, $leagueID, $name, $update));
		}
	}
	
	public function saveLeagues($leagues) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('INSERT INTO `tleague` (`country#`, `division#`, `league#`, `name`, `update`) VALUES (?, ?, ?, ?, ?) ON DUPLICATE KEY UPDATE `name`=VALUES(`name`), `update`=VALUES(`update`)')) {
			foreach ($leagues as $league) {
				$countryID = $league->getDivision()->getCountry()->getCountryID();
				$divisionID = $league->getDivision()->getDivisionID();
				$leagueID = $league->getLeagueID();
				$name = $league->getName();
				$update = date('Y-m-d H:i:s', $league->getUpdate());
				
				$stmt->execute(array($countryID, $divisionID, $leagueID, $name, $update));
			}
		}
	}
	
	public function deleteLeaguesByCountry($country) {
		$db = new DB();
		$connection = $db->connect();
		
		if ($stmt = $connection->prepare('DELETE FROM `tleague` WHERE `country#` = ?')) {
			$stmt->execute(array($country->getCountryID()));
		}
	}
		
}

?>